from core import PeaceType;
from rules.peaces.AbstractPeace import AbstractPeace;
from rules.peaces.WhitePawn import WhitePawn;
from rules.peaces.BlackPawn import BlackPawn;
from rules.peaces.VaukalakPeace import VaukalakPeace;
from rules.peaces.GetmanPeace import GetmanPeace;
from rules.peaces.LadziaPeace import LadziaPeace;
from rules.peaces.GarmataPeace import GarmataPeace;
from rules.peaces.KniazhychPeace import KniazhychPeace;
from rules.peaces.KniazPeace import KniazPeace;
from core.MotionCommand import MotionCommand;

class RulesController:

    def __init__(self, boardData):
        self._boardData = boardData;
        self.currentPlayer = None;
        self.opponentPlayer = None;
        self._peaces = {PeaceType.BLANK_CELL: AbstractPeace(self, PeaceType.BLANK_CELL),

                        PeaceType.WHITE_PAWN: WhitePawn(self, PeaceType.BLANK_CELL),
                        PeaceType.WHITE_VAUKALAK: VaukalakPeace(self, PeaceType.WHITE_VAUKALAK),
                        PeaceType.WHITE_GARMATA: GarmataPeace(self, PeaceType.WHITE_GARMATA),
                        PeaceType.WHITE_LADZIA: LadziaPeace(self, PeaceType.WHITE_LADZIA),
                        PeaceType.WHITE_GETMAN: GetmanPeace(self, PeaceType.WHITE_GETMAN),
                        PeaceType.WHITE_KNIAZ: KniazPeace(self, PeaceType.WHITE_KNIAZ),
                        PeaceType.WHITE_KNIAZHYCH: KniazhychPeace(self, PeaceType.WHITE_KNIAZHYCH),

                        PeaceType.BLACK_PAWN: BlackPawn(self, PeaceType.BLACK_PAWN),
                        PeaceType.BLACK_VAUKALAK: VaukalakPeace(self, PeaceType.BLACK_VAUKALAK),
                        PeaceType.BLACK_GARMATA: GarmataPeace(self, PeaceType.BLACK_GARMATA),
                        PeaceType.BLACK_LADZIA: LadziaPeace(self, PeaceType.BLACK_LADZIA),
                        PeaceType.BLACK_GETMAN: GetmanPeace(self, PeaceType.BLACK_GETMAN),
                        PeaceType.BLACK_KNIAZ: KniazPeace(self, PeaceType.BLACK_KNIAZ),
                        PeaceType.BLACK_KNIAZHYCH: KniazhychPeace(self, PeaceType.BLACK_KNIAZHYCH)
        };

    def getPeaceLogic(self, peaceType):
        return self._peaces[peaceType];

    def processMotion(self, sP, fP):
        if(self.testMotion(sP, fP)):
            self.boardData.rollBackAll();
            self._boardData.movePeace(sP,fP);
            return True;
        else:
            self.boardData.rollBackAll();
            return False;

    def switchPlayer(self):
        temp = self.currentPlayer;
        self.currentPlayer = self.opponentPlayer;
        self.opponentPlayer = temp;

    def testMotion(self, sP, fP):
        peaceType = self._boardData.getPeaceByCell(sP);
        logicPeace = self._peaces[peaceType];
        if(fP.x == 4 and fP.y == 4 and not logicPeace.isNobel):
            returnValue = False;
        else:
            if(logicPeace.checkMotion(sP, fP)):
                self.boardData.testCommand(MotionCommand(sP, fP,self.boardData));
                if(self.currentPlayer.isUnderCheck or self.currentPlayer.isUnderRokash):
                    returnValue = False;
                elif(self.opponentPlayer.hasNobelOnTron and not self.opponentPlayer.isUnderRokash):
                    returnValue = False;
                else:
                    returnValue = True;
            else:
                returnValue = False;
        return returnValue;

    @property
    def boardData(self):
        return self._boardData;
